package com.itheima.goods.controller.admin;


import com.itheima.goods.dto.DishDTO;
import com.itheima.goods.dto.DishPageQueryDTO;
import com.itheima.goods.pojo.Dish;
import com.itheima.goods.service.DishService;
import com.itheima.goods.vo.DishVO;
import com.itheima.result.PageBean;
import com.itheima.result.Result;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;



    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result page(DishPageQueryDTO dishPageQueryDTO){
       PageBean pageBean= dishService.select(dishPageQueryDTO);
        return Result.success(pageBean);
    }

    /**
     * 菜品起售、停售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status, Long id){
        dishService.updateStatus(status,id);
        return Result.success();
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Long id){
        DishVO dishVO= dishService.findById(id);
        return Result.success(dishVO);
    }

    @PostMapping("/{dishId}")
    public Result<Dish> findByDishId(@PathVariable  Long dishId){
        Dish dish= dishService.findByDishId(dishId);
        return Result.success(dish);
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public Result list(Long categoryId){
        List<Dish> dishes= dishService.listDish(categoryId);
        return Result.success(dishes);

    }

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    public Result addDish(@RequestBody DishDTO dishDTO){
        System.out.println(dishDTO+"=================");
        dishService.add(dishDTO);
        return Result.success();

    }

    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result delete(Long[] ids){
        System.out.println(Arrays.toString(ids)+"=============================");
        dishService.delete(ids);
        return Result.success();

    }

    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @PutMapping
    public Result updateDish(@RequestBody DishDTO dishDTO){
        dishService.updateDish(dishDTO);
        return Result.success();
    }

}
